//
//  TLMutexUIViewDelegate.m
//  KidReading
//
//  Created by telen on 15/2/5.
//  Copyright (c) 2015年 刘赞黄Telen. All rights reserved.
//

#import "TLMutexUIViewDelegate.h"

@implementation TLMutexUIViewDelegate

+ (TLMutexUIViewDelegate *)getOneMutexDelegate
{
    TLMutexUIViewDelegate* mutex = [[TLMutexUIViewDelegate alloc] init];
    return mutex;
}

- (instancetype)init
{
    if (self = [super  init]) {
        arr = [[NSMutableArray alloc] initWithCapacity:0];
    }
    return self;
}

- (void)dealloc
{
    for (NSValue* value in arr) {
        UIView<TLMutexDelegate> * view = [value nonretainedObjectValue];
        [view removeObserver:self forKeyPath:@"hidden" context:nil];
    }
}

- (void)addMutexView:(UIView<TLMutexDelegate> *)view
{
    NSValue* value = [NSValue valueWithNonretainedObject:view];
    [arr addObject:value];
    [view addObserver:self forKeyPath:@"hidden" options:(NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld) context:nil];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    BOOL hidden = [[change objectForKey:@"new"] boolValue];
    if(!hidden)for (NSValue* value in arr) {
        UIView<TLMutexDelegate> * view = [value nonretainedObjectValue];
        if (view != object) {
            [view unShow];
        }
    }
}

@end
